import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from 'src/entities/user.entity';
import { Repository } from 'typeorm';
import { RegisterDto } from './dto/register.dto';

@Injectable()
export class RegisterService {
  constructor(
    @InjectRepository(User)
    private usersRepository: Repository<User>,
  ) {}
  async create(registerDto: RegisterDto) {
    let user: User = await this.usersRepository.findOne({
      where: { eMail: registerDto.eMail },
    });
    if (user) {
      throw new HttpException('该邮箱已注册', HttpStatus.BAD_REQUEST);
    }
    return this.usersRepository.save(registerDto);
  }
}
